package com.qf.quduba.controller;/*
作者： 放学狂吃辣条
时间： 
    
*/

import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.entity.Bookrack;
import com.qf.quduba.service.BookService;
import com.qf.quduba.service.BookrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/booksrack")
public class BookrackController {

    @Autowired
    private BookrackService bookrackService;
    @Resource
    private BookService bookService;


    @GetMapping("getuserid")
    public ResultVO getBookInfo(int userId){

        List<Bookrack> bookInfo = bookrackService.findBookRack(userId);
        if(bookInfo != null){
            return new ResultVO(1000,"成功",bookInfo);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("getBook")
    public ResultVO getBook(String userId,String bookId){

        Bookrack bookrack = bookrackService.findBookRackByBookId(userId, bookId);
        if(bookrack != null){
            return new ResultVO(1000,"成功",bookrack);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }



    @GetMapping("deleterack")
    public ResultVO deleteRack(int bookrackId){

        int bookInfo = bookrackService.deleteRackId(bookrackId);
        if(bookInfo != 0){
            return new ResultVO(1000,"成功",bookInfo);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }



    @PostMapping("saveBookRack")
    public ResultVO saveBookRack(String userId,String bookId,String chapterId){
        Bookrack bookrack = bookrackService.findBookRackByBookId(userId, bookId);
        if(bookrack!=null){
            return new ResultVO(1001,"您已收藏过该书籍",null);
        }else {
            int i = bookrackService.saveBookRack(userId, bookId, chapterId);
            if(i != 0){
                int i1 = bookService.updateCollect(bookId);
                return new ResultVO(1000,"成功",null);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        }
    }
}
